<?php

class Email extends MX_Controller {

	function Email() {
		parent::__construct();
		$this->load->model('emailmodel');
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->language = $this->languagemodel->is_language_using();
	}

	function index() {
		$data['title'] = "China Property, Property for Sale/Rent, China Real Estate | Jones Lang LaSalle (CN) Residential";
		$data['description'] = "China Property - Find China real estate listings, property for sale/rent with Jone Lang LaSalle, China's Leading Property Site.";
		$data['keywords'] = "China property, China real estate, property for sale, property for rent, property guru, China property listings";

		//dd value for dropdown search
		//load dropdown value
		$data['dd_propertytype'] = $this->lookupmodel->listpropertytype();

		//load price list for local and international
		$data['dd_pricelist'] = $this->lookupmodel->listpricelist('resale_sale');
		$data['dd_pricelist_international'] = $this->lookupmodel->listpricelist('international');

		$data['dd_bedroom'] = $this->lookupmodel->listbedroom();
		$data['dd_bathroom'] = $this->lookupmodel->listbathroom();

		$data['dd_size'] = $this->lookupmodel->listsize('primary');

		$data['lkup_features'] = $this->lookupmodel->getFeatures();

		foreach ($data['lkup_features']->result() as $feature) {
			$data['lkfeatures'][] = array(
                'pkfeatureid' => $feature->pkfeatureid,
                'feature' => $feature->feature
			);
		}

		$data['lkup_facilities'] = $this->lookupmodel->getFacilities();

		foreach ($data['lkup_facilities']->result() as $faciliti) {
			$data['lkfacilities'][] = array(
                'pkfacilitiesid' => $faciliti->pkfacilitiesid,
                'facilities' => $faciliti->facilities
			);
		}


		//get international country
		$data['dd_countries'] = $this->global_lookupmodel->getcountry_international();



		//end of dd valie for dropdown search
		$this->emailmodel->sendmail("", "", "");
	}

	function contactform() {
		$this->lang->load('common/header', $this->language);
		$this->lang->load('email/email', $this->language);

		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//form id is 5 for contact form
		$admin_email_obj = $this->emailmodel->get_admin_email(16);

		//get admin email
		$admin_email = $admin_email_obj->AdminAddress;

		if (isset($_POST['action']) && $_POST['action'] == "sendenquiry") {

			$data['title'] = $this->input->post('title');
			$data['name'] = $this->input->post('firstname');
			$data['lastname'] = $this->input->post('lastname');
			$data['mobile'] = $this->input->post('mobile');
			$data['phone'] = $this->input->post('phone');
			$data['email'] = $this->input->post('email');
			$data['country'] = $this->input->post('country');
			$data['query'] = $this->input->post('comments');

			$to = $admin_email;
			$from = $this->lang->line('text_from_subscribe_local');

			$bcc = "";
			$subject = $this->lang->line('text_contact_form_inquiry') . $data['name'];

			$text = $this->lang->line('text_contact_sent_by') . $data['name'] . "\n\n";
			$text .= $this->lang->line('text_personal_particulars') . "\n ---------------------\n";
			$text .= $this->lang->line('text_title_contact_form') . ": " . $data['title'] . "\n";
			$text .= $this->lang->line('text_firstname') . ": " . $data['name'] . "\n";
			if ($data['lastname'] != "") {
				$text .= $this->lang->line('text_lastname') . ": " . $data['lastname'] . "\n";
			}
			if ($data['mobile'] != "") {
				$text .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "\n";
			}
			if ($data['phone'] != "") {
				$text .= $this->lang->line('text_phone_number') . ": " . $data['phone'] . "\n";
			}
			$text .= $this->lang->line('text_email') . ": " . $data['email'] . "\n";
			if ($data['country'] != "") {
				$text .= $this->lang->line('text_country') . ": " . $data['country'] . "\n";
			}

			$text .= $this->lang->line('text_query') . ": " . "\n -----------------\n";
			$text .= $this->lang->line('text_type_of_inquiry') . ": " . "\n " . $data['query'] . "\n\n";
			$text .= $this->lang->line('text_message_generated');

			$message = $this->lang->line('text_contact_sent_by') . $data['name'] . "<br/><br/>";
			$message .= $this->lang->line('text_personal_particulars') . ": " . "<br/> ---------------------<br/>";
			$message .= $this->lang->line('text_title_contact_form') . ": " . $data['title'] . "<br/>";
			$message .= $this->lang->line('text_firstname') . ": " . $data['name'] . "<br/>";
			if ($data['lastname'] != "") {
				$message .= $this->lang->line('text_lastname') . ": " . $data['lastname'] . "<br/>";
			}
			if ($data['phone'] != "") {
				$message .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "<br/>";
			}
			if ($data['phone'] != "") {
				$message .= $this->lang->line(text_phone_number) . ": " . $data['phone'] . "<br/>";
			}
			$message .= $this->lang->line('text_email') . ": " . $data['email'] . "<br/>";
			if ($data['country'] != "") {
				$message .= $this->lang->line('text_country') . ": " . $data['country'] . "<br/>";
			}
			$message .= $this->lang->line('text_query') . ": " . "<br/> -----------------<br/>";
			$message .= $this->lang->line('text_type_of_inquiry') . ": " . "<br/> " . $data['query'] . "<br/><br/>";
			$message .= $this->lang->line('text_message_generated');


			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);

			if ($success === true) {
				redirect('contact-us/success', 'location');
			}
		} else {
			redirect('contact', 'location');
		}
	}

	function listing_contactform() {
		$this->lang->load('common/header', $this->language);
		$this->lang->load('email/email', $this->language);

		$data['title'] = $this->lang->line('text_contact_form_title');
		$data['description'] = $this->lang->line('text_contact_form_description');
		$data['keywords'] = $this->lang->line('text_contact_form_keywords');


		if (isset($_POST['action']) && $_POST['action'] == "sendenquiry") {

			$mobile_no = $this->input->post('mobile');

			if (!is_numeric($mobile_no)) {
				//redirect('/listing/detail/'.$this->input->post('listing_id'));
			}

			$data['title'] = $this->input->post('title');
			$data['name'] = $this->input->post('name');
			$data['mobile'] = $this->input->post('mobile');
			$data['email'] = $this->input->post('email');
			$data['modeofcontact'] = $this->input->post('modeofcontact_value');
			$data['preferred_time_of_contact'] = $this->input->post('preferred_time_of_contact_value');
			$data['country'] = $this->input->post('country');
			$data['additional_qn'] = $this->input->post('additional_qn');
			$data['agentemail'] = $this->input->post('agentemail');
			$data['listing_id'] = $this->input->post('listing_id');
			$data['listing_name'] = $this->input->post('listing_name');
			$data['unitmeasurement'] = $this->input->post('unitmeasurement');

			$to = $data['agentemail'];
			$from = $this->lang->line('text_from_subscribe_local');
			$bcc = "";

			$subject = $this->lang->line('text_property_inquiry') . $data['listing_name'];


			$text = $this->lang->line('text_contact_sent_by') . $data['name'] . " - " . $data['email'] . "\n\n";
			$text .= $this->lang->line('text_personal_particulars') . ":\n ---------------------\n";
			$text .= $this->lang->line('text_title_contact_form') . ": " . $data['title'] . "\n";
			$text .= $this->lang->line('text_name') . ": " . $data['name'] . "\n";
			$text .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "\n";
			$text .= $this->lang->line('text_email') . ": " . $data['email'] . "\n";
			$text .= $this->lang->line('text_preferred_mode_of_contact') . ": " . $data['modeofcontact'] . "\n";
			$text .= $this->lang->line('text_preferred_time_of_contact') . ": " . $data['preferred_time_of_contact'] . "\n";
			$text .= $this->lang->line('text_country') . ": " . $data['country'] . "\n\n";
			$text .= $this->lang->line('text_addition_question') . ": " . "\n" . $data['additional_qn'] . "\n\n";
			$text .= $this->lang->line('text_listing_detail') . ": " . "\n -----------------\n";
			$text .= sprintf($this->lang->line('text_listing_detail_url'), base_url(), $data['unitmeasurement'], $data['listing_id'], $this->language, base_url(), $data['unitmeasurement'], $data['listing_id'], $this->language) . "\n\n";
			$text .= $this->lang->line('text_do_not_reply');


			$message = $this->lang->line('text_contact_sent_by') . $data['name'] . " - " . $data['email'] . "<br/><br/>";
			$message .= $this->lang->line('text_personal_particulars') . ": " . "<br/> ---------------------<br/>";
			$message .= $this->lang->line('text_title_contact_form') . ": " . $data['title'] . "<br/>";
			$message .= $this->lang->line('text_name') . ": " . $data['name'] . "<br/>";
			$message .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "<br/>";
			$message .= $this->lang->line('text_email') . ": " . $data['email'] . "<br/>";
			$message .= $this->lang->line('text_preferred_time_of_contact') . ": " . $data['modeofcontact'] . "<br/>";

			$message .= $this->lang->line('text_preferred_time_of_contact') . ": " . $data['preferred_time_of_contact'] . "<br/>";
			$message .= $this->lang->line('text_country') . ": " . $data['country'] . "<br/>";
			$message .= $this->lang->line('text_addition_question') . ": " ."<br/> -----------------<br/>" . $data['additional_qn'] . "<br/><br/>";
			$message .= $this->lang->line('text_listing_detail') . ": " ."<br/> -----------------<br/>";
			$message .= sprintf($this->lang->line('text_listing_detail_url'), base_url(), $data['unitmeasurement'], $data['listing_id'], $this->language, base_url(), $data['unitmeasurement'], $data['listing_id'], $this->language) . "<br/><br/>";
			$message .= $this->lang->line('text_do_not_reply');

			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
			if ($success === true) {
				redirect('listing/detail/' . $data['unitmeasurement'] . '/' . $data['listing_id'] . "/success#listing_contact", 'location');
			} else {
				redirect('listing/detail/' . $data['unitmeasurement'] . '/' . $data['listing_id'] . "/error", 'location');
			};
		} else {
			redirect(base_url());
		}
	}
	
	function listingDetailContactAgent() {
		$this->lang->load('common/header', $this->language);
		$this->lang->load('email/email', $this->language);

		$data['title'] = $this->lang->line('text_contact_form_title');
		$data['description'] = $this->lang->line('text_contact_form_description');
		$data['keywords'] = $this->lang->line('text_contact_form_keywords');
		
		if (isset($_POST['action']) && $_POST['action'] == "sendenquiry") {

			$mobile_no = $this->input->post('mobile');

			if (!is_numeric($mobile_no)) {
				//redirect('/listing/detail/'.$this->input->post('listing_id'));
			}

			$data['name'] = $this->input->post('name');
			$data['mobile'] = $this->input->post('phone');
			$data['email'] = $this->input->post('email');
			$data['agentemail'] = $this->input->post('agent_email');
			$data['listing_id'] = $this->input->post('listing_id');
			$data['listing_name'] = $this->input->post('listing_name');
			
			//increase Hit one more unit
			$this->load->model('listingmap/mdl_listingmapmodel');
			$this->mdl_listingmapmodel->updateHit($data['listing_id']);

			$to = $data['agentemail'];
			$from = $this->lang->line('text_from_subscribe_local');
			$bcc = "";

			$subject = $this->lang->line('text_property_inquiry') . $data['listing_name'];

			$text = $this->lang->line('text_contact_sent_by') . $data['name'] . " - " . $data['email'] . "\n\n";
			$text .= $this->lang->line('text_personal_particulars') . ":\n ---------------------\n";
			$text .= $this->lang->line('text_name') . ": " . $data['name'] . "\n";
			$text .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "\n";
			$text .= $this->lang->line('text_email') . ": " . $data['email'] . "\n";
			$text .= sprintf($this->lang->line('text_listing_detail_url'), base_url(), $data['listing_id'], $this->language, base_url(), $data['listing_id'], $this->language) . "\n\n";
			$text .= $this->lang->line('text_do_not_reply');


			$message = $this->lang->line('text_contact_sent_by') . $data['name'] . " - " . $data['email'] . "<br/><br/>";
			$message .= $this->lang->line('text_personal_particulars') . ": " . "<br/> ---------------------<br/>";
			$message .= $this->lang->line('text_name') . ": " . $data['name'] . "<br/>";
			$message .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "<br/>";
			$message .= $this->lang->line('text_email') . ": " . $data['email'] . "<br/>";
			$message .= $this->lang->line('text_listing_detail') . ": " ."<br/> -----------------<br/>";
			$message .= sprintf($this->lang->line('text_listing_detail_url'), base_url(), $data['listing_id'], base_url(), $data['listing_id']) . "<br/><br/>";
			$message .= $this->lang->line('text_do_not_reply');

			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
			
			$json = array();
			$json['success'] = $this->lang->line('text_sent_message');
			
			echo json_encode($json);
			
		} else {
			redirect(base_url());
		}
	}
	
	//Contact Agent Form - Find An Expert
	function FindAnExpertContactForm() {
		$this->lang->load('common/header', $this->language);
		$this->lang->load('email/email', $this->language);

		$data['title'] = $this->lang->line('text_contact_form_title');
		$data['description'] = $this->lang->line('text_contact_form_description');
		$data['keywords'] = $this->lang->line('text_contact_form_keywords');
		
		if (isset($_POST['action']) && $_POST['action'] == "sendenquiry") {

			$mobile_no = $this->input->post('mobile');

			if (!is_numeric($mobile_no)) {
				//redirect('/listing/detail/'.$this->input->post('listing_id'));
			}

			$data['name'] = $this->input->post('name');
			$data['mobile'] = $this->input->post('phone');
			$data['email'] = $this->input->post('email');
			$data['agentemail'] = $this->input->post('agent_email');
			
			//increase Hit one more unit
			$this->load->model('listingmap/mdl_listingmapmodel');

			$to = $data['agentemail'];
			$from = $this->lang->line('text_from_subscribe_local');
			$bcc = "";

			$subject = $this->lang->line('text_property_inquiry');

			$text = $this->lang->line('text_contact_sent_by') . $data['name'] . " - " . $data['email'] . "\n\n";
			$text .= $this->lang->line('text_personal_particulars') . ":\n ---------------------\n";
			$text .= $this->lang->line('text_name') . ": " . $data['name'] . "\n";
			$text .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "\n";
			$text .= $this->lang->line('text_email') . ": " . $data['email'] . "\n";
			$text .= $this->lang->line('text_do_not_reply');


			$message = $this->lang->line('text_contact_sent_by') . $data['name'] . " - " . $data['email'] . "<br/><br/>";
			$message .= $this->lang->line('text_personal_particulars') . ": " . "<br/> ---------------------<br/>";
			$message .= $this->lang->line('text_name') . ": " . $data['name'] . "<br/>";
			$message .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "<br/>";
			$message .= $this->lang->line('text_email') . ": " . $data['email'] . "<br/>";
			$message .= $this->lang->line('text_listing_detail') . ": " ."<br/> -----------------<br/>";
			$message .= $this->lang->line('text_do_not_reply');

			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
			
			$json = array();
			$json['success'] = $this->lang->line('text_sent_message');
			
			echo json_encode($json);
			
		} else {
			redirect(base_url());
		}
	}

	//Post Requirement Form

	function postrequirementform() {
		$data['title'] = $this->lang->line('text_contact_form_title');
		$data['description'] = $this->lang->line('text_contact_form_description');
		$data['keywords'] = $this->lang->line('text_contact_form_keywords');

		//form id is 2 for contact form
		$admin_email_obj = $this->emailmodel->get_admin_email(15);

		//get admin email
		$admin_email = $admin_email_obj->AdminAddress;

		if (isset($_POST['action']) && $_POST['action'] == "sendenquiry") {
			$data['propertytype'] = $this->input->post('propertytype');
			$data['city'] = $this->input->post('city');
			$data['locality'] = $this->input->post('locality');
			$data['price_min'] = $this->input->post('price_min');
			$data['price_max'] = $this->input->post('price_max');
			$data['bedroom'] = $this->input->post('bedroom');
			$data['area_min'] = $this->input->post('area_min');
			$data['area_max'] = $this->input->post('area_max');
			$data['areaunit'] = $this->input->post('areaunit');
			$data['keyfeatures'] = $this->input->post('keyfeatures');

			$data['name'] = $this->input->post('name');
			$data['mobile'] = $this->input->post('mobile');
			$data['email'] = $this->input->post('email');


			$to = $admin_email;
			$from = $this->lang->line('text_from_subscribe_local');
			$bcc = "";
			$subject = $this->lang->line('text_post_property_requirement') . $data['name'];

			$text = $this->lang->line('text_property_requirement_is_submitted_by') . $data['name'] . "\n\n";
			$text .= $this->lang->line('text_personal_particulars') . ":\n ---------------------\n";
			$text .= $this->lang->line('text_name') . ": " . $data['name'] . "\n";
			$text .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "\n";
			$text .= $this->lang->line('text_email') . ": " . $data['email'] . "\n";

			$text .= $this->lang->line('text_property_requirements') . ":\n ---------------------\n";
			$text .= $this->lang->line('text_propertytype') . ": " . $data['propertytype'] . "\n";
			$text .= $this->lang->line('text_city') . ": " . $data['city'] . "\n";
			$text .= $this->lang->line('text_location') . ": " . $data['locality'] . "\n";
			$text .= $this->lang->line('text_price') . ": " . $data['price_min'] . ' to ' . $data['price_max'] . "\n";
			$text .= $this->lang->line('text_bedrooms') . ": " . $data['bedroom'] . "\n";
			$text .= $this->lang->line('text_size') . ": " . $data['area_min'] . ' to ' . $data['area_max'] . "\n\n";

			$text .= $this->lang->line('text_key_feature') . ":\n ---------------------\n";
			$text .= $data['keyfeatures'] . "\n\n";

			$text .= $this->lang->line('text_message_generated');

			$message .= $this->lang->line('text_property_requirement_is_submitted_by') . $data['name'] . "<br/><br/>";

			$message .= $this->lang->line('text_personal_particulars') . ":<br/> ---------------------<br/>";

			$message .= $this->lang->line('text_name') . ": " . $data['name'] . "<br/>";
			$message .= $this->lang->line('text_mobile') . ": " . $data['mobile'] . "<br/>";
			$message .= $this->lang->line('text_email') . ": " . $data['email'] . "<br/><br/>";

			$message .= $this->lang->line('text_property_requirements') . ":<br/> ---------------------<br/>";
			$message .= $this->lang->line('text_propertytype') . ": " . $data['propertytype'] . "<br/>";
			$message .= $this->lang->line('text_city') . ": " . $data['city'] . "<br/>";
			$message .= $this->lang->line('text_location') . ": " . $data['locality'] . "<br/>";
			$message .= $this->lang->line('text_price') . ": " . $data['price_min'] . ' to ' . $data['price_max'] . "<br/>";
			$message .= $this->lang->line('text_bedrooms') . $data['bedroom'] . "<br/>";
			$message .= $this->lang->line('text_size') . $data['area_min'] . ' to ' . $data['area_max'] . "<br/><br/>";

			$message .= $this->lang->line('text_key_feature') . ":<br/> ---------------------<br/>";
			$message .= $data['keyfeatures'] . "<br/><br/>";

			$message .= $this->lang->line('text_message_generated');


			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);

			if ($success === true) {
				redirect('post-requirements/success', 'location');
			} else {
				$data['error'] = $success;
				$this->load->view('post-requirement', $data);
			}
		} else {
			redirect('post-requirements', 'location');
		}
	}

	//Home Loan Home
	function loanform(){
		//form id is 3 for Home Loan form
		$admin_email_obj = $this->emailmodel->get_admin_email(19);

		//get admin email
		$admin_email = $admin_email_obj->AdminAddress;


		if(isset($_POST['action']) && $_POST['action']=="sendenquiry"){
			$data['name'] = $this->input->post('name');
			$data['city'] = $this->input->post('city');
			$data['mobile'] = $this->input->post('mobile');
			$data['landline'] = $this->input->post('landline');
			$data['email'] = $this->input->post('email');
			$data['loan_amount'] = $this->input->post('loan_amount');
				
				
			$to= $admin_email;
			$from="Jones Lang LaSalle Residential Indonesia";
			$bcc="";
			$subject="[Home Loan Inquiry] Jones Lang LaSalle Residential - From " . $data['name'];
				
			$text = "The following home loan inquiry is sent by " . $data['name'] .  "\n\n";
			$text .= "Personal Particulars:\n ---------------------\n";
			$text .= "Name: " . $data['name'] . "\n";
			$text .= "City: " . $data['city'] . "\n";
			$text .= "Mobile: " . $data['mobile'] . "\n";
			$text .= "Landline: " . $data['landline'] . "\n";
			$text .= "Email: " . $data['email'] . "\n";
			$text .= "Loan Amount: " . $data['loan_amount'] . "\n\n";
			$text .= "This message is generated and sent via JLL Residential Portal, Home Loan form feature. Please do not reply to this email.";
				
				
			$message = "The following home loan inquiry is sent by " . $data['name'] .  "<br/><br/>";
			$message .= "Personal Particulars:<br/> ---------------------<br/>";
			$message .= "Name: " . $data['name'] . "<br/>";
			$message .= "City: " . $data['city'] . "<br/>";
			$message .= "Mobile: " . $data['mobile'] . "<br/>";
			$message .= "Landline: " . $data['landline'] . "<br/>";
			$message .= "Email: " . $data['email'] . "<br/>";
			$message .= "Loan Amount: " . $data['loan_amount'] . "<br/><br/>";
			$message .= "This message is generated and sent via JLL Residential Portal, Home Loan form feature. Please do not reply to this email.";
				
			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
				
			if($success === TRUE){
				redirect('homeloan/success','location');
			}
				
		}else{
			redirect('homeloan','location');
		}
	}
}

